
<template>
  <a-card :bordered="false" :class="'cust-erp-sub-tab'">
    <!-- 操作按钮区域 -->
    <div class="table-operator" v-if="mainId">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="url.importExcelUrl"
        @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-button @click="handleExportXls" type="primary" icon="download" style="margin-left:8px">导出</a-button>

      <a-button
        @click="batchDel"
        style="margin-left:8px"
        v-show="selectedRowKeys.length > 0"
        ghost
        type="primary"
        icon="delete">批量删除</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="pagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <oufuform-erp-child-modal ref="modalForm" @ok="modalFormOk" :foreignKeys="main.foreignKeys" :code="main.cgRpConfigId" :mainId="mainId"></oufuform-erp-child-modal>
  </a-card>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import OufuformErpChildModal from './modules/OufuformErpChildModal'
  export default {
    name: "OufuformErpChildList",
    mixins:[JeecgListMixin],
    components: { OufuformErpChildModal },
    props:{
      mainId:{
        type:String,
        default:'',
        required:false
      },
      main:{
        type:Object,
        default:{},
        required:false
      },
      parentCode:{
        type:String,
        default:'',
        required:false
      },
    },
    watch:{
      mainId:{
        immediate: true,
        handler(val) {
          if(!this.mainId){
            this.clearList()
          }else{
            this.main.foreignKeys.forEach(key=>{
              this.queryParam[key.field] = val
            })
            this.queryParam['autoHeadId'] = this.main.cgRpConfigId
            this.queryParam['autoParentHeadId'] = this.parentCode
            this.loadData(1);
          }
        }
      }
    },
    data () {
      return {
        description: this.main.description+'管理页面',
        disableMixinCreated:true,
        indexColumns: {
          title: '#',
          dataIndex: '',
          key:'rowIndex',
          width:60,
          align:"center",
          customRender:function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        actionColumns:{
          title: '操作',
          dataIndex: 'action',
          align:"center",
          scopedSlots: { customRender: 'action' },
        },
        // 分页参数
        pagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        // 表头
        columns: [],
        url: {
          list: "/online/cgform/public/listSubById",
          delete: '/online/cgform/public/autoDeleteForm',
          exportXlsUrl: '/online/cgform/autoExportXls',
          importExcelUrl: '/online/cgform/autoImportXls?mainId='+this.mainId,
        },
      }
    },
    created(){
        this.columns = [];
        this.columns.push(this.indexColumns);
        let currColumns = this.main.columns
        currColumns.forEach(po=>{
            po.align = 'center';
            if(po.fieldShowType == 'date'){
              po.customRender = function (text) {
                return !text?"":(text.length>10?text.substr(0,10):text)
              }
            }else if(po.fieldShowType == 'Blob'){
              po.dataIndex = po.dataIndex + 'String';
            }else if(po.fieldShowType == 'umeditor'){
              po.scopedSlots =  {customRender: 'htmlSlot'}
            }else if(po.fieldShowType == 'file'){
              po.scopedSlots =  {customRender: 'fileSlot'}
            }else if(po.fieldShowType == 'image'){
              po.scopedSlots =  {customRender: 'imgSlot'}
            }else if(po.fieldShowType == 'list'||po.fieldShowType == 'radio'||po.fieldShowType == 'list_multi'||po.fieldShowType == 'checkbox'||po.fieldShowType == 'sel_search'||po.fieldShowType == 'cat_tree'||po.fieldShowType == 'sel_depart'){
              po.dataIndex =  po.dataIndex + '_dictText';
              po.customRender = function (text) {
                return text
              }
//              let dictCode = po.customRender;
//              po.customRender = (text) => {
//                  return filterMultiDictText(this.dictOptions[dictCode], text);
//              }

            }
            this.columns.push(po);
        })
        this.columns.push(this.actionColumns)
    },
    methods: {
      clearList(){
        this.dataSource=[]
        this.selectedRowKeys=[]
        this.pagination.current = 1
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>